package com.demo;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.context.properties.EnableConfigurationProperties;

import org.springframework.context.annotation.Bean;
import org.springframework.context.event.ApplicationEventMulticaster;
import org.springframework.context.event.SimpleApplicationEventMulticaster;
import org.springframework.context.support.AbstractApplicationContext;
import org.springframework.core.env.Environment;


import java.util.concurrent.Executor;

@EnableConfigurationProperties(MyProperties.class)
public class Lab2AutoConfig {

    @Autowired
    @Qualifier("myAsyncThreadPool")
    private Executor myExecutor;

    //自定义事件发布器 bean名字必须为applicationEventMulticaster,切记!!
    @Bean(AbstractApplicationContext.APPLICATION_EVENT_MULTICASTER_BEAN_NAME)
    ApplicationEventMulticaster myApplicationEventMulticaster(){
        SimpleApplicationEventMulticaster eventMulticaster = new SimpleApplicationEventMulticaster();
        eventMulticaster.setTaskExecutor(myExecutor);
        return eventMulticaster;
    }

    @Bean
    @ConditionalOnProperty(prefix = "lsb.autoconfig",name = "enable",havingValue = "true")
    CommandLineRunner getMyCommandLineRunner(Environment environment){
        return new MyCommandLineRunner(environment);
    }
}

